/**    
 * 文件名：ApprovalRest.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年10月10日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.ws;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes.CommonResultCode;
import com.logic.landseaserver.common.util.DateBetweenObject;
import com.logic.landseaserver.common.util.DateBetweenUtil;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.common.util.LoggerHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：CommonRest</br>
 * 类描述：通用</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年10月10日 下午4:39:20</br>
 * @version 1.0
 *
 */
@Path("/common")
@Api(value = "通用Rest")
public class CommonRest
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CommonRest.class);
    
    /**
     * 
     * [简要描述]：获取审核流程</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-10-12)</br>
     *
     * @param request
     * @return
     *
     */
    @POST
    @Path("/getDateBetween")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "计算两个日期相差年/月/天")
    public Response getDateBetween(@ApiParam(value = "param", required = true) Map<String,Object> requestMap)
    {
        CommonResultMap map = new CommonResultMap();
        
        try
        {
            String startTimeStr = (String)requestMap.get("startTime");
            String endTimeStr = (String)requestMap.get("endTime");
            
            Map<String,Object> mapResult = new LinkedHashMap<String,Object>();
            if (StringUtils.isEmpty(startTimeStr) || StringUtils.isEmpty(endTimeStr))
            {
                throw LandseaException.createException(CommonResultCode.E00050003);
            }
            
            Date start = DateUtil.convertDate(startTimeStr);
            Date end = DateUtil.convertDate(endTimeStr);
            DateBetweenObject dateBetweenObject  = DateBetweenUtil.process(start, end);
            
            mapResult.put("yearList", dateBetweenObject.getYearList());
            mapResult.put("monthList", dateBetweenObject.getMonthList());
            mapResult.put("dayList", dateBetweenObject.getDayList());
            
            map.setData(mapResult);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getDateBetween", e, map);
        }
        catch (Exception e)
        {
            LOGGER.error("getDateBetween handle failed.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
